********************************
***Fix Message Vary Source***
********************************

clear all

********************
*Set Person Working*
*Change this to your name!!!!!
********************
*global carolina 0
*global emily 1

global emily 1
global carolina 0

*********************
*Load Files*
*********************

*Carolina's Computer*
if $carolina ==1  {
cap cd "~\Dropbox\Carolina-Emily-Project\Data\Study"
global out "..\..\Results\"
}


if $emily ==1  {
*Emily's Computer*
cap cd "~/Dropbox/SourceContent/Data/Study"
global out "../../Results/"
} 

use 3_clean_data, replace


/*Sample KSmirnov Code*
gen groupk=1 if treatment==1
replace groupk=0 if  treatment==0
ksmirnov ln_prob_level_anti  if democrat==0 & (treatment==1 | treatment==0), by(group)

r(p_1) is probability that the first group distribution is less than the second group
r(p_2) is probability that the first group distribution is greater than the second group
r(p) gives combined probability of difference
*/

*********************************
***Create Variables and Format***
*********************************

global lasso0 hispanic age55_64 candidate16_hillary candidate16_other ///
	immlevel_decrease gun_lessstrict abortion_illegal tax_toohigh health_notgvt ///
	occ_twitter occ_buzzfeed trumpfan lebronfan taylorfan bgatesfan ///
	obamafan obama_neutral topissue_health 
	
global lasso1 black hispanic age35_44 age45_54 hsdegree ///
	candidate16_hillary immlevel_decrease immlevel_same ///
	gun_same gun_lessstrict abortion_partlegal abortion_illegal tax_toohigh ///
	health_neutral health_notgvt i.freq_nytimes ///
	daily_tv occ_tv daily_newspaper i.freq_fox week_breitbart ///
	daily_breitbart occ_buzzfeed ///
	lebron_neutral obamafan trump_neutral trumpfan west topissue_tax 

	
/*Order or Treatment Groups*
5 7 6 8 1 3 2 4 9 10
*/


gen group1=treatment==5 | treatment==9 | treatment==0
gen group2=treatment==6 | treatment==10 | treatment==0
gen group3=treatment==1 | treatment==9 | treatment==0
gen group4=treatment==2 | treatment==10 | treatment==0

global message1 "Anti"
global message2 "Anti"
global message3 "Pro"
global message4 "Pro"

global source1 "Trump"
global source2 "Obama"
global source3 "Trump"
global source4 "Obama" 

foreach y in 1 3 {
	global space`y' "[0.1em]" 
	} 
foreach y in 2 {
	global space`y' "[1em]" 
	} 
foreach y in 4 {
	global space`y' "[0.65em]" 
	} 
	
gen president=treatment==1 | treatment==2 | treatment==5 | treatment==6
gen actor=treatment==3 | treatment==4 | treatment==7 | treatment==8
gen turkey= treatment==9 | treatment==10
gen priming=turkey==1 | president==1
gen message=actor==1 | president==1

global color1 "blue"
global color0 "red"

*gen fan_president=obamafan if treatment==6 | treatment==8 | treatment==2 | treatment==4
*replace fan_president=trumpfan if treatment==5 | treatment==7 | treatment==1 | treatment==3


*****************************
*****************************
*****************************
global party0 "Republicans"
global party1 "Democrats"
global var0 "ln(Probability Anti-Immigrant + 1)"
global var1 "ln(Probability Pro-Immigrant + 1)"
global rule0 "\toprule"
global rule1 "\midrule"
global dir0 "anti"
global dir1 "pro"
global Dir0 "Anti"
global Dir1 "Pro"


****************************************
*TABLES - Pro/Anti VERSION*
****************************************

	
file close _all
capture macro drop fh
file open fh using "${out}5_SourceFixed_VaryMessage.tex", write replace
file write fh ///
"\hspace*{-1cm}\small{\begin{tabular}{llccccccccc}"  _n 

foreach p in 0 1 {

sum prob_index_${dir`p'} if treat==0 & recruit==`p'
local y`p'=round(`r(mean)'*1000)/1000
local y`p': di %6.3f `y`p''

sum ln1_prob_index_${dir`p'} if treat==0 & recruit==`p'
local ly`p'=round(`r(mean)'*1000)/1000
local ly`p': di %6.3f `ly`p''
	
file write fh ///
"${rule`p'}" _n ///
" & & \multicolumn{9}{c}{\textbf{${party`p'}}}   \\ [0.75em] " _n ///
" & & \multicolumn{9}{c}{\textit{${var`p'}}}   \\ [0.2em] " _n 

file write fh ///
" & & \multicolumn{4}{c}{Source Priming} & \multicolumn{4}{c}{Identified Message} & \\" _n ///
" \textbf{Message} & \textbf{Source} & $\beta_s$ &  (S.E.) & \%Diff. & \textit{P(Dist.)} & $\beta_{sm}$ &  (S.E.) & \%Diff. & \textit{P(Dist.)} &  N \\" _n ///
"\midrule \\" _n	

foreach y in 1 2 3 4 {
 
	**************************
	*Decomposition Regression*
	**************************

	reg ln1_prob_index_${dir`p'} priming president ///
		${lasso`p'} if (group`y'==1 ) & recruit==`p', robust

	count if e(sample)
	local n`p'=r(N)

	*Beta S - priming*	
	local bs`p' = round(_b[priming]*1000)/1000
	local bs`p' : di %6.3f `bs`p''
	local ss`p' = round(_se[priming]*1000)/1000
	local ss`p' : di  %6.3f `ss`p''
	local p`p' = 2*ttail(e(df_r),abs(_b[priming]/_se[priming]))
	if `p`p''<=0.1 & `p`p''>0.05 {
		global stars`p' "*"
		}
	if `p`p''<=0.05 & `p`p''>0.01 {
		global stars`p' "**"
		}
	if `p`p''<=0.01 {
		global stars`p' "***"
		}
	else if `p`p''>0.1 {
		global stars`p' ""
		}
	sum prob_index_${dir`p'} if treatment==0 & e(sample)==1
	local m=r(mean)
	local cs`p'=round(_b[priming]*(`m'+1)/`m'*10000)/100
	local cs`p' : di  %6.2f `cs`p''

	
	*Beta MS - President*	
	local bsm`p' = round(_b[president]*1000)/1000
	local bsm`p' : di %6.3f `bsm`p''
	local ssm`p' = round(_se[president]*1000)/1000
	local ssm`p' : di  %6.3f `ssm`p''
	local p`p' = 2*ttail(e(df_r),abs(_b[president]/_se[president]))
	if `p`p''<=0.1 & `p`p''>0.05 {
		global starsm`p' "*"
		}
	if `p`p''<=0.05 & `p`p''>0.01 {
		global starsm`p' "**"
		}
	if `p`p''<=0.01 {
		global starsm`p' "***"
		}
	else if `p`p''>0.1 {
		global starsm`p' ""
		}
	sum prob_index_${dir`p'} if president==0 & turkey==1 & e(sample)==1
	local m=r(mean)
	local csm`p'=round(_b[president]*(`m'+1)/`m'*10000)/100
	local csm`p' : di  %6.2f `csm`p''
	
	*Distribution Difference - Actor vs. Control*
	reg ln1_prob_index_${dir`p'} ${lasso`p'} ///
		if (group`y'==1 & president==0)  & recruit==`p'
	predict res, res
	gen g=0 if priming==0 & e(sample)==1
	replace g=1 if priming==1 & e(sample)==1
	ksmirnov res if e(sample), by(g)
	local ds`p'=round(r(p)*1000)/1000
	local ds`p': di %6.3f `ds`p''
	drop g res

	
	*Distribution Difference - President vs. Actor*
	reg ln1_prob_index_${dir`p'} ${lasso`p'} if (group`y'==1 & treatment!=0) & recruit==`p'
	predict res, res
	gen g=0 if president==0 & e(sample)==1
	replace g=1 if president==1 & e(sample)==1
	ksmirnov res if e(sample), by(g)
	local dsm`p'=round(r(p)*1000)/1000
	local dsm`p': di %6.3f `dsm`p''
	drop g res
	
	file write fh ///
	"${message`y'} & ${source`y'} & `bs`p''${stars`p'} & (`ss`p'')  & `cs`p''\% & \textit{`ds`p''} & `bsm`p''${starsm`p'} & (`ssm`p'') & `csm`p''\% & \textit{`dsm`p''} & `n`p'' 	\\" _n ///
	"${space`y'}" _n	
	
}

file write fh ///
"  \multicolumn{11}{r}{Control Group Mean: \textit{ln(P(${Dir`p'})+1)}=`ly`p'', \textit{P(${Dir`p'})}=`y`p''}   \\ \\ " _n 

}

file write fh ///
"\bottomrule" _n ///
"\end{tabular}}\hspace*{-1cm}" _n
file close fh









